#include "cmdinswartimetest.h"
#include "mainctrl.h"

#include <stdio.h>
#include "ConfigFile.hpp"

CPPUNIT_TEST_SUITE_REGISTRATION(CCmdInsWarTimeTest);

extern CMainCtrl g_stctrl;

void CCmdInsWarTimeTest::setUp()
{
	const char* pszConfig = "(512,20000)(1024,5000)(4096,4000)(65536,3000)";
	size_t iSize = CT_CMDFACTORY->CountSize(pszConfig);
	char *pszBuffer = new char[iSize];
	CT_CMDFACTORY->Init(pszConfig,pszBuffer,iSize);
	m_pstCmdInsWarTime = (CCmdInsWarTime*)CT_CMDFACTORY->CreateCmd(ID_INS_WAR);
	PrepareData();
}

void CCmdInsWarTimeTest::PrepareData()
{
	//建造城市才能组建部队
	m_iMaxCityID = 1;
	m_pstCmdInsWarTime->m_pstMain = &g_stctrl;
	g_stctrl.pstCityHash->SetNextNewCityID(&m_iMaxCityID);

	int iUin = 6266052;
	int iUinTarget = 88883613;

	
	m_stCity.iCityOwner = iUin;
	m_stCity.iGridID = 1021;
	int iRet = g_stctrl.CreateCity(m_stCity);
	g_stctrl.m_stWorldMap.UseGrid(m_stCity.iGridID,EGT_CITY,m_stCity.iCityID,false);

	
	m_stCityTarget.iCityOwner = iUinTarget;
	m_stCityTarget.iGridID = 1020;
	iRet = g_stctrl.CreateCity(m_stCityTarget);
	g_stctrl.m_stWorldMap.UseGrid(m_stCityTarget.iGridID,EGT_CITY,m_stCityTarget.iCityID,false);

	//有用户才能组建部队
	CPlayer stPlayer;
	stPlayer.iUin = iUin;
	stPlayer.bIsNewPlayer = true;
	g_stctrl.CreatePlayer(stPlayer);

	CPlayer stPlayerTarget;
	stPlayerTarget.iUin = iUinTarget;
	stPlayerTarget.bIsNewPlayer = true;
	g_stctrl.CreatePlayer(stPlayerTarget);


	//用户的英雄和宝物需要设定并加载到内存当中来
	CTArray<CUserHero, EMAX_USER_HERONUM>  astHeros;
	astHeros.m_iUsedCount = 0;
	iRet = g_stctrl.m_stHeroTreaMaster.addHeros(iUin,astHeros);
	iRet = g_stctrl.m_stHeroTreaMaster.addHeros(iUinTarget,astHeros);

	CTArray<CUserTrea, EMAX_USER_TREANUM> astTrea;
	astTrea.m_iUsedCount = 0;
	iRet = g_stctrl.m_stHeroTreaMaster.addTreas(iUin,astTrea);
	iRet = g_stctrl.m_stHeroTreaMaster.addTreas(iUinTarget,astTrea);
}

void CCmdInsWarTimeTest::tearDown()
{
	m_pstCmdInsWarTime->Done(RET_OK);
}

void CCmdInsWarTimeTest::testTest()
{

}

void CCmdInsWarTimeTest::testSimpleDo()
{

	char *pszSectionName = "RobWar";
	//组建部队
	STRWATERID stNewWater;
	CCityHash::HASH_PTR pstCityHashPtr = g_stctrl.GetCityInfo(m_stCity.iCityID);
	pstCityHashPtr()->GetBuildWaterID(&stNewWater,WATER_EAT(EAT_ROBWAR));
	CCarmyHash::HASH_PTR pstCarmyPtr = g_stctrl.GetAllocArmy(stNewWater,false);
	

	//设置军队属性
	pstCarmyPtr()->ClearArmy();
	pstCarmyPtr()->stCarmyWaterID = stNewWater;
	pstCarmyPtr()->iLeaveCityID = m_stCity.iCityID;
	pstCarmyPtr()->iTargetCityID = m_stCityTarget.iCityID;
	pstCarmyPtr()->iTargetiGridID = m_stCityTarget.iGridID;
	pstCarmyPtr()->iType = EAT_ROBWAR;
	pstCarmyPtr()->iStat = EAS_CREATE;
	pstCarmyPtr()->iUin = m_stCity.iCityOwner;

	pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER_COOLY,0);     ///< 粮草兵
	pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER_TRUCK,0);	  ///< 运输车
	pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER_SPY,0);		  ///< 侦查兵
	pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER_SETTLER,0);	  ///< 拓荒兵
	pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER_TARGE,0);	  ///< 枪盾兵
	pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER_ONAGER,0);	  ///< 弩弓兵
	pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER_LORICAE,0);	  ///< 重甲将军
	pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER_SPEAR,0);	  ///< 长枪兵
	pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER_SABER,0);	  ///< 骑枪战将
	pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER_BOW,0);		  ///< 弓箭兵
	pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER_DRAGOON,0);	  ///< 骑射战将

	CConfigFile stTestConfig;
	CPPUNIT_ASSERT(stTestConfig.OpenFile("./unittest.ini") == 0);
	
	char szLine[1024] = {0};
	stTestConfig.GetItemValue(pszSectionName,"Army",szLine,sizeof(szLine),"");
	//设置进攻军队人数
	int astArmy[EBI_BUILD_SOLDIER_END - EBI_BUILD_SOLDIER] = {0};
	
	sscanf(szLine,"%d%d%d%d%d%d%d%d%d%d%d",
		&astArmy[EBI_BUILD_SOLDIER_COOLY - EBI_BUILD_SOLDIER],
		&astArmy[EBI_BUILD_SOLDIER_TRUCK - EBI_BUILD_SOLDIER],
		&astArmy[EBI_BUILD_SOLDIER_SPY - EBI_BUILD_SOLDIER],
		&astArmy[EBI_BUILD_SOLDIER_SETTLER - EBI_BUILD_SOLDIER],
		&astArmy[EBI_BUILD_SOLDIER_TARGE - EBI_BUILD_SOLDIER],
		&astArmy[EBI_BUILD_SOLDIER_ONAGER - EBI_BUILD_SOLDIER],
		&astArmy[EBI_BUILD_SOLDIER_LORICAE - EBI_BUILD_SOLDIER],
		&astArmy[EBI_BUILD_SOLDIER_SPEAR - EBI_BUILD_SOLDIER],
		&astArmy[EBI_BUILD_SOLDIER_SABER - EBI_BUILD_SOLDIER],
		&astArmy[EBI_BUILD_SOLDIER_BOW - EBI_BUILD_SOLDIER],
		&astArmy[EBI_BUILD_SOLDIER_DRAGOON - EBI_BUILD_SOLDIER]);

	int i = 0;
	for (i = 0 ; i < EBI_BUILD_SOLDIER_DRAGOON - EBI_BUILD_SOLDIER + 1; ++i)
	{
		pstCarmyPtr()->stArmy.setSold(EBI_BUILD_SOLDIER + i,astArmy[i]);
	}

	pstCarmyPtr()->ileaveTime = time(NULL);
	pstCarmyPtr()->iStat = EAS_LEAVE;

	//设置进攻方的科技加成
	CPlayerHash::HASH_PTR pstPlayer = g_stctrl.GetPlayerInfo(pstCityHashPtr()->iCityOwner,false);
	CPPUNIT_ASSERT(pstPlayer.IsValid());

	stTestConfig.GetItemValue(pszSectionName,"Tech",szLine,sizeof(szLine),"");
	int iTechEDGE = 0,iTechArrow = 0;
	sscanf(szLine,"%d%d",&iTechEDGE,&iTechArrow);

	ATTECH stTech;
	stTech[EBI_BUILD_TECH_EDGE - EBI_BUILD_TECH] = iTechEDGE;
	stTech[EBI_BUILD_TECH_ARROW - EBI_BUILD_TECH] = iTechArrow;
	pstPlayer()->stTech.achTech = stTech;

	//设置防守方的人数
	stTestConfig.GetItemValue(pszSectionName,"ArmyTarget",szLine,sizeof(szLine),"");
	int astArmyTarget[EBI_BUILD_SOLDIER_END - EBI_BUILD_SOLDIER] = {0};
	sscanf(szLine,"%d%d%d%d%d%d%d%d%d%d%d",
		&astArmyTarget[EBI_BUILD_SOLDIER_COOLY - EBI_BUILD_SOLDIER],
		&astArmyTarget[EBI_BUILD_SOLDIER_TRUCK - EBI_BUILD_SOLDIER],
		&astArmyTarget[EBI_BUILD_SOLDIER_SPY - EBI_BUILD_SOLDIER],
		&astArmyTarget[EBI_BUILD_SOLDIER_SETTLER - EBI_BUILD_SOLDIER],
		&astArmyTarget[EBI_BUILD_SOLDIER_TARGE - EBI_BUILD_SOLDIER],
		&astArmyTarget[EBI_BUILD_SOLDIER_ONAGER - EBI_BUILD_SOLDIER],
		&astArmyTarget[EBI_BUILD_SOLDIER_LORICAE - EBI_BUILD_SOLDIER],
		&astArmyTarget[EBI_BUILD_SOLDIER_SPEAR - EBI_BUILD_SOLDIER],
		&astArmyTarget[EBI_BUILD_SOLDIER_SABER - EBI_BUILD_SOLDIER],
		&astArmyTarget[EBI_BUILD_SOLDIER_BOW - EBI_BUILD_SOLDIER],
		&astArmyTarget[EBI_BUILD_SOLDIER_DRAGOON - EBI_BUILD_SOLDIER]);


	CCityHash::HASH_PTR pstCityTarget = g_stctrl.GetCityInfo(m_stCityTarget.iCityID);

	for (i = 0 ; i < EBI_BUILD_SOLDIER_DRAGOON - EBI_BUILD_SOLDIER + 1; ++i)
	{
		pstCityTarget()->stArmy.setSold(EBI_BUILD_SOLDIER + i,astArmyTarget[i]);
	}

	//设置防守方的科技加成
	stTestConfig.GetItemValue(pszSectionName,"TechTarget",szLine,sizeof(szLine),"");
	int iTechArmor = 0;
	sscanf(szLine,"%d",&iTechArmor);

	ATTECH stTechTarget;
	stTechTarget[EBI_BUILD_TECH_ARMOR - EBI_BUILD_TECH] = iTechArmor;
	CPlayerHash::HASH_PTR pstPlayerTarget = g_stctrl.GetPlayerInfo(pstCityTarget()->iCityOwner,false);

	//设置防守方的城防加成
	stTestConfig.GetItemValue(pszSectionName,"FortTarget",szLine,sizeof(szLine),"");	
	int astFort[EBI_BUILD_FORT_END - EBI_BUILD_FORT] = {0};
	sscanf(szLine,"%d%d%d%d%d%d%d%d",
		&astFort[EBI_BUILD_FORT_PIT - EBI_BUILD_FORT],
		&astFort[EBI_BUILD_FORT_SNARE - EBI_BUILD_FORT],
		&astFort[EBI_BUILD_FORT_SENTRY - EBI_BUILD_FORT],
		&astFort[EBI_BUILD_FORT_TOWER - EBI_BUILD_FORT],
		&astFort[EBI_BUILD_FORT_ROCK - EBI_BUILD_FORT],
		&astFort[EBI_BUILD_FORT_LOG - EBI_BUILD_FORT],
		&astFort[EBI_BUILD_FORT_FIRE - EBI_BUILD_FORT],
		&astFort[EBI_BUILD_FORT_WINCH - EBI_BUILD_FORT]);

	for (i = 0; i < EBI_BUILD_FORT_WINCH - EBI_BUILD_FORT + 1; ++i)
	{
		pstCityTarget()->stFort.AddFort(i,astFort[i]);
	}

	//设置防守方的粮食、资源等
	int iLastUpdateTime = 0;
	stTestConfig.GetItemValue(pszSectionName,"LastUpdateTime",iLastUpdateTime,0);
	pstCityTarget()->iLastUpdateTime = iLastUpdateTime;

	stTestConfig.GetItemValue(pszSectionName,"FoodTarget",szLine,sizeof(szLine),"");
	sscanf(szLine,"%d%d%d%d%d",
		&pstCityTarget()->iFoodCount,&pstCityTarget()->iFoodExpend,&pstCityTarget()->iFoodMax,
		&pstCityTarget()->iFoodPerhour,(int*)&pstCityTarget()->iNoFoodTime);

	stTestConfig.GetItemValue(pszSectionName,"WoodTarget",szLine,sizeof(szLine),"");
	sscanf(szLine,"%d%d%d",
		&pstCityTarget()->iWoodCount,&pstCityTarget()->iWoodMax,&pstCityTarget()->iWoodPerhour);

	stTestConfig.GetItemValue(pszSectionName,"IronTarget",szLine,sizeof(szLine),"");
	sscanf(szLine,"%d%d%d",
		&pstCityTarget()->iIronCount,&pstCityTarget()->iIronMax,&pstCityTarget()->iIronPerhour);

	stTestConfig.GetItemValue(pszSectionName,"StoneTarget",szLine,sizeof(szLine),"");
	sscanf(szLine,"%d%d%d",
		&pstCityTarget()->iStoneCount,&pstCityTarget()->iStoneMax,&pstCityTarget()->iStonePerhour);

	//int iFoodCount = pstCityTarget()->iFoodCount;
	//int iWoodCount = pstCityTarget()->iWoodCount;
	//int iIronCount = pstCityTarget()->iIronCount;
	//int iStoneCount = pstCityTarget()->iStoneCount;


	m_pstCmdInsWarTime->m_stArmyPtr = pstCarmyPtr;
	int iRet = m_pstCmdInsWarTime->Do(NULL);

	CPPUNIT_ASSERT(iRet == 0);
	CPPUNIT_ASSERT(pstCarmyPtr()->iStat == EAS_RETURN);
	for (i = 0; i < 20 ; ++i)
	{
		int iSoldierCount = pstCarmyPtr()->stArmy.GetSoldCount(i+EBI_BUILD_SOLDIER);
		CPPUNIT_ASSERT( iSoldierCount <= astArmy[i] );
		CPPUNIT_ASSERT( iSoldierCount >= 0);
	}

	for (i = 0 ; i < 20 ; ++i)
	{
		int iSoldierCount = pstCityTarget()->stArmy.GetSoldCount(i+EBI_BUILD_SOLDIER);
		CPPUNIT_ASSERT( iSoldierCount <= astArmyTarget[i]);
		CPPUNIT_ASSERT( iSoldierCount >= 0);
	}

	int iFoodCurrent = pstCityTarget()->iFoodCount;
	int iWoodCurrent = pstCityTarget()->iWoodCount;
	int iIronCurrent = pstCityTarget()->iIronCount;
	int iStoneCurrent = pstCityTarget()->iStoneCount;

	CPPUNIT_ASSERT(iFoodCurrent >=0 );
//	CPPUNIT_ASSERT(iFoodCurrent <= iFoodCount);
	CPPUNIT_ASSERT(iWoodCurrent >=0);
//	CPPUNIT_ASSERT(iWoodCurrent <= iWoodCount);
	CPPUNIT_ASSERT(iIronCurrent >=0);
//	CPPUNIT_ASSERT(iWoodCurrent <= iIronCount);
	CPPUNIT_ASSERT(iStoneCurrent >=0);
//	CPPUNIT_ASSERT(iStoneCurrent <= iStoneCount);
}

